package github.kawaiior.yuanchuxiuzhen.common.tileentity;

import github.kawaiior.yuanchuxiuzhen.Yuanchuxiuzhen;
import github.kawaiior.yuanchuxiuzhen.common.container.MaterialConverterContainer;
import github.kawaiior.yuanchuxiuzhen.common.item.misc.ManaStoneItem;
import github.kawaiior.yuanchuxiuzhen.core.init.ItemInit;
import github.kawaiior.yuanchuxiuzhen.core.init.TileEntityInit;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;

import javax.annotation.Nullable;

public class MaterialConverterTileEntity extends TileEntity implements ITickableTileEntity, INamedContainerProvider {

    private int tickTime = 0;

    public static final int LENGTH = 9;
    private Inventory inventory = new Inventory(LENGTH);
    private Inventory shopInventory = new Inventory(LENGTH);

    private static final String inventoryString = "converter_";
    private static final String shopInventoryString = "shop_converter_";

    public MaterialConverterTileEntity(TileEntityType<?> tileEntityTypeIn) {
        super(tileEntityTypeIn);
    }

    public MaterialConverterTileEntity() {
        this(TileEntityInit.MATERIAL_CONVERTER_TILE_ENTITY.get());
    }

    @Override
    public ITextComponent getDisplayName() {
        return new TranslationTextComponent("container."+ Yuanchuxiuzhen.MODID+".example_gui_block");
    }

    @Nullable
    @Override
    public Container createMenu(int id, PlayerInventory playerInventory, PlayerEntity playerEntity) {
        return new MaterialConverterContainer(id, playerInventory, this);
    }

    @Override
    public void tick() {
        tickTime++;
        if (tickTime<20){
            return;
        }
        tickTime = 0;

        int maxLevel = 0;
        for (int i = 0; i < LENGTH; i++) {
            ItemStack itemStack = this.shopInventory.getStackInSlot(i);
            if (itemStack.isEmpty() || !(itemStack.getItem() instanceof ManaStoneItem)){
                continue;
            }
            maxLevel = ((ManaStoneItem)itemStack.getItem()).getLevel();
        }

        this.onSlotHasManaStone(maxLevel);
    }

    private void onSlotHasManaStone(int level){
        switch (level) {
            case 1:
                this.inventory.setInventorySlotContents(0, new ItemStack(ItemInit.FINE_IRON.get()));
                this.inventory.setInventorySlotContents(1, new ItemStack(ItemInit.CLOUD_CLOTH.get()));
                this.inventory.setInventorySlotContents(2, new ItemStack(ItemInit.FIRE_STONE.get()));
                this.inventory.setInventorySlotContents(3, new ItemStack(ItemInit.JADEITE_ITEM.get()));
                this.inventory.setInventorySlotContents(4, new ItemStack(ItemInit.GOURD_ITEM.get()));
                this.inventory.setInventorySlotContents(5, new ItemStack(ItemInit.HEART_CUT_PAPER.get()));
                break;
            case 2:
                this.inventory.setInventorySlotContents(0, new ItemStack(ItemInit.BITTER_BAMBOO.get()));
                this.inventory.setInventorySlotContents(1, new ItemStack(ItemInit.JADE_WOOD.get()));
                this.inventory.setInventorySlotContents(2, new ItemStack(ItemInit.BLACK_STONE.get()));
                this.inventory.setInventorySlotContents(3, new ItemStack(ItemInit.IRON_METEORITE.get()));
                break;
            case 3:
                this.inventory.setInventorySlotContents(0, new ItemStack(ItemInit.METEORITE_STEEL.get()));
                this.inventory.setInventorySlotContents(1, new ItemStack(ItemInit.FROST_IRON.get()));
                this.inventory.setInventorySlotContents(2, new ItemStack(ItemInit.METEORITE_ROCK.get()));
                this.inventory.setInventorySlotContents(3, ItemStack.EMPTY);
                break;
            case 4:
                this.inventory.setInventorySlotContents(0, new ItemStack(ItemInit.CONCEALED_STEEL.get()));
                this.inventory.setInventorySlotContents(1, new ItemStack(ItemInit.BURNING_COPPER.get()));
                this.inventory.setInventorySlotContents(2, new ItemStack(ItemInit.PEAK_SKIN.get()));
                this.inventory.setInventorySlotContents(3, ItemStack.EMPTY);
                break;
            case 5:
                this.inventory.setInventorySlotContents(0, new ItemStack(ItemInit.COLD_POND_STEEL.get()));
                this.inventory.setInventorySlotContents(1, new ItemStack(ItemInit.BONE_BREAK_STEEL.get()));
                this.inventory.setInventorySlotContents(2, new ItemStack(ItemInit.BLOOD_SILK.get()));
                this.inventory.setInventorySlotContents(3, ItemStack.EMPTY);
                break;
            case 6:
                this.inventory.setInventorySlotContents(0, new ItemStack(ItemInit.ANCIENT_ALLOY.get()));
                this.inventory.setInventorySlotContents(1, new ItemStack(ItemInit.MELTING_ALLOY.get()));
                this.inventory.setInventorySlotContents(2, new ItemStack(ItemInit.NIHILISM_SKIN.get()));
                this.inventory.setInventorySlotContents(3, ItemStack.EMPTY);
                break;
            case 7:
                this.inventory.setInventorySlotContents(0, new ItemStack(ItemInit.PEAK_SILVER.get()));
                this.inventory.setInventorySlotContents(1, new ItemStack(ItemInit.PURE_BLUE_GOLD.get()));
                this.inventory.setInventorySlotContents(2, new ItemStack(ItemInit.VITALITY_STONE.get()));
                this.inventory.setInventorySlotContents(3, ItemStack.EMPTY);
                break;
            case 8:
                this.inventory.setInventorySlotContents(0, new ItemStack(ItemInit.DARK_THUNDER_GOLD.get()));
                this.inventory.setInventorySlotContents(1, new ItemStack(ItemInit.NOON_SILVER.get()));
                this.inventory.setInventorySlotContents(2, ItemStack.EMPTY);
                this.inventory.setInventorySlotContents(3, ItemStack.EMPTY);
                break;
            case 9:
                this.inventory.setInventorySlotContents(0, new ItemStack(ItemInit.SHINING_GOLD.get()));
                this.inventory.setInventorySlotContents(1, new ItemStack(ItemInit.CHAOTIC_FUR.get()));
                this.inventory.setInventorySlotContents(2, ItemStack.EMPTY);
                this.inventory.setInventorySlotContents(3, ItemStack.EMPTY);
                break;
            default:
                this.inventory.setInventorySlotContents(0, ItemStack.EMPTY);
                this.inventory.setInventorySlotContents(1, ItemStack.EMPTY);
                this.inventory.setInventorySlotContents(2, ItemStack.EMPTY);
                this.inventory.setInventorySlotContents(3, ItemStack.EMPTY);
                break;
        }
    }

    @Override
    public void read(BlockState state, CompoundNBT nbt) {
        for (int i = 0; i < LENGTH; i++) {
            this.inventory.setInventorySlotContents(i, ItemStack.read(nbt.getCompound(inventoryString+i)));
            this.shopInventory.setInventorySlotContents(i, ItemStack.read(nbt.getCompound(shopInventoryString+i)));
        }

        super.read(state, nbt);
    }

    @Override
    public CompoundNBT write(CompoundNBT compound) {
        for (int i = 0; i < LENGTH; i++) {
            ItemStack itemStack = this.inventory.getStackInSlot(i).copy();
            compound.put(inventoryString+i, itemStack.serializeNBT());

            ItemStack shopItemStack = this.shopInventory.getStackInSlot(i).copy();
            compound.put(shopInventoryString+i, shopItemStack.serializeNBT());
        }

        return super.write(compound);
    }

    public Inventory getInventory() {
        return inventory;
    }

    public Inventory getShopInventory(){
        return shopInventory;
    }
}
